#include <QDebug>
#include <QButtonGroup>
#include <QPushButton>
#include <QTextDocument>
#include "CalcMainView.h"

CalcMainView::CalcMainView(QWidget *parent)
    : QWidget(parent),
    mKeyboard(NULL),
    mCurrentMode(BasicMode),
    mScientificmodeKeyboard(NULL),
    mBasicmodeKeyboard(NULL),
    mProgrammodeKeyboard(NULL),
    mStatisticsmodeKeyboard(NULL)
{
    mMainLayout = new QVBoxLayout(this);
    mMainLayout->setDirection(QVBoxLayout::BottomToTop);
    setLayout(mMainLayout);
    mKeyboardLayout = new QHBoxLayout(this);
    mKeyboardLayout->setDirection(QHBoxLayout::RightToLeft);
    mMainLayout->addLayout(mKeyboardLayout);
    mByteConsole = new QLabel(this);
    mByteConsole->setVisible(false); // this shd be visible only in prg mode.
    mMainLayout->addWidget(mByteConsole);
    mConsole = new QTextEdit(this);
    QTextDocument* d = mConsole->document();
    d->setDefaultTextOption(QTextOption(Qt::AlignRight));
    mConsole->setDocument(d);
    mConsole->setFixedHeight(50);
    mConsole->setReadOnly(true);
    mConsole->setText("0");
    mDatasetListWidget = new QListWidget(this);
    mDatasetListWidget->setVisible(false);
    mMainLayout->addWidget(mConsole);
    mMainLayout->addWidget(mDatasetListWidget);
    mLogic = new CalcLogic(this);
    connect(mLogic,SIGNAL(resultAvailable(QString)),mConsole,SLOT(setText(QString)),Qt::UniqueConnection);
    // this creates different types of keyboards depending on mode.
    mKeyboard = new Keyboard(this);
    handleBasicmode();
//    setFixedSize(600,270);
    this->setMaximumHeight(600);
    show();
}

CalcMainView::~CalcMainView()
{

}

void CalcMainView::handleBasicmode()
{
    // if scientific mode keyboard is visible just make it invisible
    if(mScientificmodeKeyboard)
        mScientificmodeKeyboard->setVisible(false);
    if(mProgrammodeKeyboard)
        mProgrammodeKeyboard->setVisible(false);
    if(mStatisticsmodeKeyboard && mStatisticsmodeKeyboard->isVisible())
    {
        mStatisticsmodeKeyboard->setVisible(false);
        mBasicmodeKeyboard->setVisible(true);
    }
    mLogic->setMode(BasicMode);
    QWidget* basicKeyboard = mKeyboard->basicKeyboard();
    mBasicmodeKeyboard = basicKeyboard;
    mKeyboardLayout->addWidget(basicKeyboard);
    mConsole->clear();
    mConsole->setText("0");
    connect(mKeyboard->basicKeyboardButtongroup(),SIGNAL(buttonClicked(QAbstractButton*)),
            mLogic,SLOT(buttonPressed(QAbstractButton*)),Qt::UniqueConnection);
    mByteConsole->setVisible(false);
    mDatasetListWidget->setVisible(false);
}

void CalcMainView::handleScientificmode()
{
    if(mProgrammodeKeyboard && mProgrammodeKeyboard->isVisible()) {
        mProgrammodeKeyboard->setVisible(false);
    }
    if(mStatisticsmodeKeyboard && mStatisticsmodeKeyboard->isVisible()) {
        mStatisticsmodeKeyboard->setVisible(false);
        mBasicmodeKeyboard->setVisible(true);
    }
    mLogic->setMode(ScientificMode);
    mScientificmodeKeyboard = mKeyboard->scientificKeyboard();
    mScientificmodeKeyboard;
    mKeyboardLayout->addWidget(mScientificmodeKeyboard);
    mConsole->clear();
    mConsole->setText("0");
    connect(mKeyboard->scientificKeyboardButtongroup(),SIGNAL(buttonClicked(QAbstractButton*)),
            mLogic,SLOT(buttonPressed(QAbstractButton*)),Qt::UniqueConnection);
    //for degres
    connect(mKeyboard,SIGNAL(degreesClicked()),mLogic,SLOT(degreesSelected()),Qt::UniqueConnection);
    connect(mKeyboard,SIGNAL(radiansClicked()),mLogic,SLOT(radiansSelected()),Qt::UniqueConnection);
    connect(mKeyboard,SIGNAL(gradsClicked()),mLogic,SLOT(gradsSelected()),Qt::UniqueConnection);
    //for braces
    connect(mKeyboard,SIGNAL(openBraceClicked()),mLogic,SLOT(openbraceSelected()),Qt::UniqueConnection);
    connect(mKeyboard,SIGNAL(closeBraceClicked()),mLogic,SLOT(closebraceSelected()),Qt::UniqueConnection);
    connect(mLogic,SIGNAL(updateBraceCounter(QString)),mKeyboard,SLOT(setBraceCounterText(QString)),Qt::UniqueConnection);
    mScientificmodeKeyboard->setVisible(true);
    mByteConsole->setVisible(false);
    mDatasetListWidget->setVisible(false);
}

void CalcMainView::handleProgrammode()
{
    if(mScientificmodeKeyboard && mScientificmodeKeyboard->isVisible()) {
        mScientificmodeKeyboard->setVisible(false);
    }
    if(mStatisticsmodeKeyboard && mStatisticsmodeKeyboard->isVisible()) {
        mStatisticsmodeKeyboard->setVisible(false);
        mBasicmodeKeyboard->setVisible(true);
    }
    mLogic->setMode(ProgramMode);
    QWidget* programKeyboard = mKeyboard->programKeyboard();
    mProgrammodeKeyboard = programKeyboard;
    mKeyboardLayout->addWidget(programKeyboard);
    mConsole->clear();
    mConsole->setText("0");
    connect(mKeyboard->programKeyboardButtongroup(),SIGNAL(buttonClicked(QAbstractButton*)),
            mLogic,SLOT(buttonPressed(QAbstractButton*)),Qt::UniqueConnection);
    connect(mKeyboard,SIGNAL(hexButtonClicked(QAbstractButton*)),
            mLogic,SLOT(buttonPressed(QAbstractButton*)),Qt::UniqueConnection);
    //for decimal radix buttons
    connect(mKeyboard,SIGNAL(decClicked()),mLogic,SLOT(handleDecClicked()),Qt::UniqueConnection);
    connect(mKeyboard,SIGNAL(binClicked()),mLogic,SLOT(handleBinClicked()),Qt::UniqueConnection);
    connect(mKeyboard,SIGNAL(octClicked()),mLogic,SLOT(handleOctClicked()),Qt::UniqueConnection);
    connect(mKeyboard,SIGNAL(hexClicked()),mLogic,SLOT(handleHexClicked()),Qt::UniqueConnection);
    //for qword radix buttons
    connect(mKeyboard,SIGNAL(qwordClicked()),mLogic,SLOT(handleQWordClicked()),Qt::UniqueConnection);
    connect(mKeyboard,SIGNAL(dwordClicked()),mLogic,SLOT(handleDWordClicked()),Qt::UniqueConnection);
    connect(mKeyboard,SIGNAL(wordClicked()),mLogic,SLOT(handleWordClicked()),Qt::UniqueConnection);
    connect(mKeyboard,SIGNAL(byteClicked()),mLogic,SLOT(handleByteClicked()),Qt::UniqueConnection);
    //for braces
    connect(mKeyboard,SIGNAL(openBraceClicked()),mLogic,SLOT(openbraceSelected()),Qt::UniqueConnection);
    connect(mKeyboard,SIGNAL(closeBraceClicked()),mLogic,SLOT(closebraceSelected()),Qt::UniqueConnection);
    connect(mLogic,SIGNAL(updateBraceCounter(QString)),mKeyboard,SLOT(setBraceCounterText(QString)),Qt::UniqueConnection);
    mProgrammodeKeyboard->setVisible(true);
    mByteConsole->setVisible(true);
    connect(mLogic,SIGNAL(byteResultAvailable(QString)),mByteConsole,SLOT(setText(QString)),Qt::UniqueConnection);
    mByteConsole->setText("0");
    mByteConsole->setAlignment(Qt::AlignRight);
    mDatasetListWidget->setVisible(false);
    //d->setDefaultTextOption(QTextOption(Qt::AlignRight));// update bytes in this console only.
    // mByteConsole->setText("byte console testing"); // update bytes in this console only.
}

void CalcMainView::handleStatisticsmode()
{
    if(mScientificmodeKeyboard && mScientificmodeKeyboard->isVisible()) {
        mScientificmodeKeyboard->setVisible(false);
    }
    if(mProgrammodeKeyboard && mProgrammodeKeyboard->isVisible()) {
        mProgrammodeKeyboard->setVisible(false);
    }
    if(mBasicmodeKeyboard && mBasicmodeKeyboard->isVisible()) {
        mBasicmodeKeyboard->setVisible(false);
    }
    if (mStatisticsmodeKeyboard){
        mStatisticsmodeKeyboard->setVisible(true);
    }
    mLogic->setMode(StatisticsMode);
    QWidget* statisticskeyboard = mKeyboard->statisticsKeyboard();
    mStatisticsmodeKeyboard = statisticskeyboard;
    mKeyboardLayout->addWidget(statisticskeyboard);
    mConsole->clear();
    mConsole->setText("0");
    connect(mKeyboard->statisticsKeyboardButtongroup(),SIGNAL(buttonClicked(QAbstractButton*)),
            mLogic,SLOT(buttonPressed(QAbstractButton*)),Qt::UniqueConnection);
    connect(mLogic,SIGNAL(addDatasetItem(QString)),this,SLOT(appendToDataset(QString)),Qt::UniqueConnection);
    connect(mLogic,SIGNAL(clearDataset()),this,SLOT(clearDataset()),Qt::UniqueConnection);
    mByteConsole->setVisible(false);
    mDatasetListWidget->setVisible(true);
}

void CalcMainView::appendToDataset(QString data) {
    if(StatisticsMode == mLogic->mode()) {
        QListWidgetItem* item = new QListWidgetItem(data,mDatasetListWidget);
        item->setTextAlignment(Qt::AlignRight);
        mDatasetListWidget->addItem(item);
    }
}

void CalcMainView::removeFromDataset(int rowToRemove) {
    if(StatisticsMode == mLogic->mode() && mDatasetListWidget->count() <= rowToRemove) {
        mDatasetListWidget->removeItemWidget(mDatasetListWidget->itemAt(rowToRemove,0));
    }
}

void CalcMainView::clearDataset() {
    if(StatisticsMode == mLogic->mode()) {
        mDatasetListWidget->clear();
    }
}
